<script setup>
	
import { ref } from 'vue';
import { getPasswordLogin } from '@/apis/user';
import { useUserStore } from '/stores';
//保存表单响应式数据
	const formData=ref({
		password:'',
		mobile:''
	})
	
	const form=ref()
	//校验规则
	const rules={
		mobile:{
			rules:[
				{ required: true, errorMessage: '请输入手机号' },
				{ pattern: /^1[3-9]\d{9}$/, errorMessage: '手机号格式不正确' },
			]
		},
		password:{
			rules:[
				{ required: true, errorMessage: '请输入密码' },
				 { pattern: '^[a-zA-Z0-9]{8}$', errorMessage: '密码格式不正确' }
			]
		}
	}
	
	const store=useUserStore()
	//勾选我同意
	const isAgree=ref(false)
	//登录按钮
	const onLogin=async()=>{
		if(!isAgree.value) {
			return uni.showToast({
			title:'请勾选我同意',
			icon:'error'
		})}
		try{
			await form.value.validate()
			const res=await getPasswordLogin(formData.value)
			//用户信息持久化
			store.setUserStore(res.data)
			//提示用户
			uni.showToast({
				title:'登录成功'
			})
			//跳转页面
			uni[store.openType]({
				url:store.redireUrl
			})
		}catch(e){
			console.log(e)
		}
	}
	//勾选框
	const changeAgree=()=>{
		isAgree.value=!isAgree.value	
	}
	
	
</script>
<template>
  <uni-forms class="login-form" ref="form" :model="formData" :rules="rules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
				v-model="formData.mobile"
      />
    </uni-forms-item>
    <uni-forms-item name="password">
      <uni-easyinput
        type="password"
        placeholder="请输入密码"
        :input-border="false"
        placeholder-style="color: #C3C3C5"
				v-model="formData.password"
      />
    </uni-forms-item>
    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="changeAgree"/>
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="onLogin">登 录</button>
    <navigator hover-class="none" class="uni-navigator" url=" ">
      忘记密码？
    </navigator>
  </uni-forms>
</template>

<script>
export default {
  options: {
    styleIsolation: 'shared'
  }
}
</script>

<style lang="scss">
@import './styles.scss';
</style>